package cn.edu.csust.coursecard.utils.storage;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Component
@Data
public class QiNiuFileUtil {

    @Resource
    private QiniuUtils qiniuUtils;


    public String getNewFileName(MultipartFile file) {
        return FileUtils.createFileName(file);
    }

    public String getNewFileName() {
        return FileUtils.createFileName();
    }

    public void uploadFileToQiniu(MultipartFile file, String newFileName) {
        qiniuUtils.upload(file, newFileName);
    }

    public void uploadFileToQiniu(byte[] bytes, String newFileName) {
        qiniuUtils.upload(bytes, newFileName);
    }


    public void deleteFileFromQiniu(String fileName) {
        if (StringUtils.isNotBlank(fileName)) {
            qiniuUtils.delete(fileName);
        }
    }

    public String getQiniuPrefix() {
        return qiniuUtils.getUrl();
    }


}
